package com.dqhl.business.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.business.R;
import com.dqhl.business.modle.GoodsManager;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.ViewHolder;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guanluocang on 2016/11/4.
 */
public class GoodsManagerAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsManager> goodsManagersList;
    private LayoutInflater inflater;
    private OnOperationListener operationListener;

    public GoodsManagerAdapter(Context context, List<GoodsManager> goodsManagersList,
                               OnOperationListener onOperationListener) {
        this.context = context;
        this.operationListener = onOperationListener;
        this.goodsManagersList = goodsManagersList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return goodsManagersList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.layout_good_manager, viewGroup, false);
        }
        ImageView iv_goodsPic = ViewHolder.get(view, R.id.iv_goodsPic);
        TextView tv_order_num = ViewHolder.get(view, R.id.tv_order_num);
        TextView tv_goodsName = ViewHolder.get(view, R.id.tv_goodsName);
        TextView tv_goodsNum = ViewHolder.get(view, R.id.tv_goodsNum);
        TextView tv_unit = ViewHolder.get(view, R.id.tv_unit);
        TextView tv_goodsContent = ViewHolder.get(view, R.id.tv_goodsContent);
        TextView tv_goodsPrice = ViewHolder.get(view, R.id.tv_goodsPrice);
        ImageView iv_goodsOperation = ViewHolder.get(view, R.id.iv_goodsOperation);

        GoodsManager goodsManager = goodsManagersList.get(i);

        tv_order_num.setText(goodsManager.getId());
        tv_goodsName.setText(goodsManager.getName());
        tv_goodsNum.setText(goodsManager.getStock());
        tv_unit.setText(goodsManager.getUnit());
        tv_goodsPrice.setText(goodsManager.getPrice());
        tv_goodsContent.setText(goodsManager.getInfo());

        String pic_a = goodsManager.getPic_a();
        String pic_b = goodsManager.getPic_b();
        String pic_c = goodsManager.getPic_c();
        String pic_d = goodsManager.getPic_d();
        List<String> picList = new ArrayList<>();
        if (!TextUtils.isEmpty(pic_a)) {
            picList.add(pic_a);
        }
        if (!TextUtils.isEmpty(pic_b)) {
            picList.add(pic_b);
        }
        if (!TextUtils.isEmpty(pic_c)) {
            picList.add(pic_c);
        }
        if (!TextUtils.isEmpty(pic_d)) {
            picList.add(pic_d);
        }
        if (picList != null && picList.size() > 0) {
            x.image().bind(iv_goodsPic, Config.img_url + picList.get(0));
        } else {
            iv_goodsPic.setImageResource(R.drawable.ic_no_picture);
        }

        iv_goodsOperation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operationListener.onOperation(i);
            }
        });
        return view;
    }

    public interface OnOperationListener {
        void onOperation(int position);
    }
}
